/**
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2008，所有权利保留。
 * 
 * 项目名： rumba
 * 文件名： CartridgeViewPanel.java
 * 模块说明：    
 * 修改历史：
 * Feb 18, 2008 - linhui - 创建。
 */
package com.hd123.rumba.gwt.cartridge.client;

import java.util.List;

import com.google.gwt.user.client.Element;
import com.gwtext.client.core.XTemplate;
import com.gwtext.client.data.ArrayReader;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.MemoryProxy;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.DataView;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.event.DataViewListenerAdapter;
import com.gwtext.client.widgets.layout.FitLayout;
import com.hd123.rumba.gwt.base.client.util.GWTUtil;
import com.hd123.rumba.gwt.cartridge.client.biz.QCartridge;

/**
 * 构架包显示部件 | 显示部件
 * 
 * @author Linhui
 *
 */
public class CartridgeViewPanel extends Panel {
  private final static String BORDERSTYLE = "border: transparent;background: transparent;";
  private List cartridges;
  private String[] templates;
  private String picSpec;

  public CartridgeViewPanel() {
    super();
    initViewPanel();
    picSpec = "logo_big";
  }

  private void initViewPanel() {
    templates = new String[]{
        "<tpl for='.'>",
        "<div class='thumb-wrap'>",
        "<div class='thumb'><img src='{url}' ext:qtip='{name}'></div>",
        "</div></tpl>",
        "<div class='x-clear'></div>"
    };
  }

  public void setCartridges(List data) {
    this.cartridges = data;
  }
  
  public void setAlignRight(boolean isAlignRight) {
    if(isAlignRight == true)
      templates[1] = "<div class='thumb-wrap-right'>";
    else
      templates[1] = "<div class='thumb-wrap'>";
  }
  
  public void setPictureSpec(String spec) {
    picSpec = spec;
  }
  /**
   * 转化构架包QCartridge列表信息
   * 
   * @return Ext的Strore模型所需的Object二维数组
   */
  private Object[][] getConvertedData() {
    if(cartridges.size() == 0)
      return null;
    Object[][] memory = new Object[cartridges.size()][];
    for(int i = 0 ; i < cartridges.size(); i++) {
      QCartridge cartridge = (QCartridge)cartridges.get(i);
      memory[i] = new Object[4];
      memory[i][0] = cartridge.getCaptionEN() + " " + cartridge.getCaption();
      memory[i][1] = cartridge.getVersion().toString();
      memory[i][2] = cartridge.getCopyright();
      memory[i][3] = GWTUtil.getStaticContentURL(
          (String)cartridge.getResources().get(picSpec));
    }
    return memory;
  }

  /**
   * 显示已经构造好的构架包显示列表的Panel
   * 
   * @return 该Panel本身
   */
  public CartridgeViewPanel showCartridges() {
      this.setBodyStyle(BORDERSTYLE);
      this.clear();
      DataView dataView = genDataView();

      Panel inner = new Panel();
      
      inner.setWidth("100%");
      inner.setId("cartridge-view");
      inner.setBodyStyle(BORDERSTYLE);
      inner.setAutoHeight(true);
      inner.setHeader(false);
      inner.setCollapsible(false);
      inner.setLayout(new FitLayout());
      inner.setTitle("您已安装了以下产品：");
      inner.add(dataView);
      this.add(inner);
      return this;
  }

  private DataView genDataView() {
    MemoryProxy dataProxy = new MemoryProxy(getConvertedData());
    RecordDef recordDef = new RecordDef(new FieldDef[]{
        new StringFieldDef("caption"),
        new StringFieldDef("version"),
        new StringFieldDef("copyright"),
        new StringFieldDef("url"),
    });
    ArrayReader reader = new ArrayReader(recordDef);

    final Store store = new Store(dataProxy, reader, true);
    store.load();

    DataView dataView = new DataView("div.thumb-wrap") {
      public void prepareData(Data data) {
        data.setProperty("name", data.getProperty("caption") + "<br>版本号：" +
            data.getProperty("version") + "<br>" +
            data.getProperty("copyright"));
//        data.setProperty("shortName", Format.ellipsis(data.getProperty("name"), 4));
      }
    };
    dataView.addListener(new DataViewListenerAdapter() {
      public void onSelectionChange(DataView view, Element[] selections) {
//        if(view.getSelectionCount() > 0){
//          Record record = view.getSelectedRecords()[0];
//          Panel panel = new Panel();
//          panel.setShadow(true);
//
//          panel.setHtml("<p>" + record.getAsString("name") + "</p>" +
//              "<p>" + record.getAsString("version") + "</p>" +
//              "<p>" + record.getAsString("remark") + "</p>"
//          );
//
//          final Window window = new Window();
//          window.setTitle(record.getAsString("name"));
//          window.setResizable(true);
//          window.setLayout(new FitLayout());
//          window.setWidth(350);
//          window.setModal(false);
//          window.add(panel);
//
//          window.setAnimateTarget("cartridge-view");
//          window.show();
//
//        }
        super.onSelectionChange(view, selections);
      }
    });
    dataView.setWidth("100%");
    dataView.setStore(store);
    dataView.setTpl(new XTemplate(templates));
    dataView.setAutoHeight(true);
    dataView.setSingleSelect(true);
//    dataView.setOverCls("x-view-over");
    dataView.setEmptyText("未定义图片");
    return dataView;
  }
}
